/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package control;

import com.opensymphony.xwork2.ActionSupport;
import java.util.Date;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import org.apache.struts2.ServletActionContext;

import java.util.List;
import pojo.Category;
import pojo.Reply;
import pojo.Thread;
import pojo.User;
import pojo.Pointdispatchhistory;
import service.IReplyService;
import service.IThreadService;
import service.IUserService;
import service.IPointService;
import service.ICategoryService;

/**
 *
 * @author administrator
 */
public class ThreadAction extends ActionSupport {

    private IThreadService<Thread> threadService;
    private IReplyService<Reply> replyService;
    private IUserService<User> userService;
    private ICategoryService<Category> categoryService;
    private IPointService<Pointdispatchhistory> pointService;
    private List<Thread> threads;
    private List<Thread> tops;
    private List<Reply> replies;
    private Thread thread;
    private String replyContent;
    private int threadID;
    private int categoryID;
    private String categoryName;
    private int now = 1;
    private int pageSize = 10;
    private int rowCount;
    private int pageCount;
    private String findName;
    private User user;

    public ICategoryService<Category> getCategoryService() {
        return categoryService;
    }

    public void setCategoryService(ICategoryService<Category> categoryService) {
        this.categoryService = categoryService;
    }


    /**
     * @author: Yu
     * add a user property for the Thread class
     */

    public User getUser() {
        return user;
    }

    public void setUser(User user) {
        this.user = user;
    }

    public IUserService<User> getUserService() {
        return userService;
    }

    public void setUserService(IUserService<User> userService) {
        this.userService = userService;
    }

    public IPointService<Pointdispatchhistory> getPointService() {
        return pointService;
    }

    public void setPointService(IPointService<Pointdispatchhistory> pointService) {
        this.pointService = pointService;
    }

//******************************************************


    


    public String getFindName() {
        return findName;
    }

    public void setFindName(String findName) {
        this.findName = findName;
    }

    public List<Thread> getTops() {
        return tops;
    }

    public void setTops(List<Thread> tops) {
        this.tops = tops;
    }


    public String getReplyContent() {
        return replyContent;
    }

    public void setReplyContent(String replyContent) {
        this.replyContent = replyContent;
    }

    public List<Reply> getReplies() {
        return replies;
    }

    public IReplyService<Reply> getReplyService() {
        return replyService;
    }

    public void setReplyService(IReplyService<Reply> replyService) {
        this.replyService = replyService;
    }

    public void setReplies(List<Reply> replies) {
        this.replies = replies;
    }

    public Thread getThread() {
        return thread;
    }

    public void setThread(Thread thread) {
        this.thread = thread;
    }

    public int getThreadID() {
        return threadID;
    }

    public void setThreadID(int threadID) {
        this.threadID = threadID;
    }

    public String getCategoryName() {
        return categoryName;
    }

    public void setCategoryName(String categoryName) {
        this.categoryName = categoryName;
    }

    public int getNow() {
        return now;
    }

    public void setNow(int now) {
        this.now = now;
    }

    public int getPageCount() {
        return pageCount;
    }

    public void setPageCount(int pageCount) {
        this.pageCount = pageCount;
    }

    public int getPageSize() {
        return pageSize;
    }

    public void setPageSize(int pageSize) {
        this.pageSize = pageSize;
    }

    public int getRowCount() {
        return rowCount;
    }

    public void setRowCount(int rowCount) {
        this.rowCount = rowCount;
    }

    public int getCategoryID() {
        return categoryID;
    }

    public void setCategoryID(int categoryID) {
        this.categoryID = categoryID;
    }

    public IThreadService<Thread> getThreadService() {
        return threadService;
    }

    public void setThreadService(IThreadService<Thread> threadService) {
        this.threadService = threadService;
    }

    public List<Thread> getThreads() {
        return threads;
    }

    public void setThreads(List<Thread> threads) {
        this.threads = threads;
    }

    public String listThreads() {
        HttpServletRequest request = ServletActionContext.getRequest();
        HttpSession session = request.getSession(true);
        session.setAttribute("categoryName", this.categoryName);
        session.setAttribute("categoryID", this.categoryID);
        this.rowCount = this.threadService.count(this.categoryID);
        int offset = this.calOffset();
        request.setAttribute("level", (now-1)*pageSize);
        this.threads = this.threadService.listThread(this.categoryID, offset, this.pageSize);
        this.tops = this.threadService.listTop(this.categoryID, offset, this.pageSize);
        return "listThreads";
    }

    public String listRecommends() {
        HttpServletRequest request = ServletActionContext.getRequest();
        HttpSession session = request.getSession(true);
        session.setAttribute("categoryName", this.categoryName);
        session.setAttribute("categoryID", this.categoryID);
        this.rowCount = this.threadService.countRecommend(this.categoryID);
        int offset = this.calOffset();
        request.setAttribute("level", (now-1)*pageSize);
        this.threads = this.threadService.listRecommend(this.categoryID, offset, this.pageSize);
        this.tops = this.threadService.listTop(this.categoryID, offset, this.pageSize);
        return "listRecommends";
    }

    public int calOffset() {
        this.pageCount = (this.rowCount + this.pageSize - 1) / this.pageSize;
        if(this.pageCount == 0)
            this.pageCount = 1;
        if (this.now < 1) {
            this.now = 1;
        }
        if (this.now > this.pageCount) {
            this.now = this.pageCount;
        }
        int offset = (this.now - 1) * this.pageSize;
        if (offset < 0) {
            offset = 0;
        }
        return offset;
    }

    public String viewThread() {
        HttpServletRequest request = ServletActionContext.getRequest();
        HttpSession session = request.getSession(true);
        session.setAttribute("categoryName", this.categoryName);
        session.setAttribute("categoryID", this.categoryID);
        this.thread = this.threadService.GetThread(this.threadID);
        if (this.thread != null) {
            this.rowCount = this.replyService.count(this.threadID);
            int offset = this.calOffset();
            this.replies = this.replyService.listReply(this.threadID, offset, this.pageSize);
            this.threadService.IncreaseViewTimes(this.thread);
            return "viewThread";
        }
        return "error";
    }
/*
    public String SearchThread() {
        HttpServletRequest request = ServletActionContext.getRequest();
        HttpSession session = request.getSession(true);
        this.thread = this.threadService.GetThread(findName);
        if (this.thread != null) {
            request.setAttribute("threadTitle", this.thread.getThreadTitle());
            request.setAttribute("threadContext", this.thread.getThreadContext());
            request.setAttribute("author", this.thread.getUser().getUnikey());
            request.setAttribute("postDate", this.thread.getPostDate());
            this.replies = this.replyService.listReply(this.threadID);
            return "viewThread";
        }
        return "error";
    }*/
    public String findThreads() {
        HttpServletRequest request = ServletActionContext.getRequest();
        HttpSession session = request.getSession(true);
        session.setAttribute("findName", this.findName);
        this.rowCount = this.threadService.count(this.findName);
        int offset = this.calOffset();
        this.threads = this.threadService.findThread(this.findName, offset, this.pageSize);
        return "listThreads";
    }

    public String postReply() {
        HttpServletRequest request = ServletActionContext.getRequest();
        HttpSession session = request.getSession(true);
        Reply newReply = new Reply();
        newReply.setReplyContext(this.replyContent);
        newReply.setReplyDate(new Date());
        this.categoryID = Integer.parseInt(session.getAttribute("categoryID").toString());
        this.categoryName = session.getAttribute("categoryName").toString();
        this.thread = this.threadService.GetThread(this.threadID);
        newReply.setThread(this.thread);
        this.user = (User) session.getAttribute("user");
        newReply.setUser(this.user);
        this.userService.increasePoint(user, 2);
        this.replyService.createNewReply(newReply);
        this.threadService.IncreaseReplyTimes(this.thread);
        Pointdispatchhistory point = new Pointdispatchhistory();
        point.setPointDispatchAmount(2);
        point.setPointDispatchDate(new Date());
        point.setPointDispatchReason("reply a thread");
        point.setUser(user);
        pointService.createNewHistory(point);
        return this.viewThread();
    }

    public String viewHistory() {
        HttpServletRequest request = ServletActionContext.getRequest();
        HttpSession session = request.getSession(true);
        this.rowCount = this.threadService.countHistory(session.getAttribute("uni").toString());
        int offset = this.calOffset();
        request.setAttribute("level", (now-1)*pageSize);
        this.threads = this.threadService.listHistory(session.getAttribute("uni").toString()
                , offset, this.pageSize);
        return "listHistory";
    }

    public String viewDetail() {
        HttpServletRequest request = ServletActionContext.getRequest();
        HttpSession session = request.getSession(true);
        session.setAttribute("categoryName", this.categoryName);
        session.setAttribute("categoryID", this.categoryID);
        return this.viewThread();
    }


    /**
     * @author: Yu
     * @return
     * process info from newThread.jsp
     * insert a new thread into the database.
     */
    public String newThread(){
        HttpServletRequest request = ServletActionContext.getRequest();
        String title = request.getParameter("title");
        String content = request.getParameter("content");
        int length = content.length();
        //content = content.substring(3, length - 4);

        // test if I can get info from the submition : success
        //System.out.println(title + " : " + content);
        HttpSession session = request.getSession(false);
        
        if (null != session){
            this.categoryName = (String) session.getAttribute("categoryName");
            this.categoryID = (Integer) session.getAttribute("categoryID");
            this.user = (User) session.getAttribute("user");
        }else{
            System.out.println("**********************************************");
            System.out.println("**  The Session Contains no Category Info.  **");
            System.out.println("**********************************************");

            return "error";
        }
        
        
        // test if I can get info from the session : success
        //System.out.println(this.categoryName + " : " + this.categoryID);
        // if I don't use the code from line 274, the session is null

        // test if I can get User from the session : success
        //System.out.println(user.getEmail());

        // test if I can get the total number of the threads : error
        //System.out.println("The total number of threads currently is : " + this.threadService.countTotalNumberOfThread());

        Category category = this.categoryService.getCategory(this.categoryID);
        Date newDate = new Date();

        Thread newThread = new Thread();
        newThread.setCategory(category);
        newThread.setUser(this.user);
        newThread.setThreadTitle(title);
        newThread.setThreadContext(content);
        newThread.setPostDate(newDate);
        newThread.setReply(true);
        newThread.setTop(false);
        newThread.setReplyTimes(0);
        newThread.setHighlight(false);
        newThread.setViewTimes(0);
        newThread.setDigest(false);
        newThread.setLastReply(newDate);

        this.threadService.createThread(newThread);
        this.userService.increasePoint(user, 5);
        this.categoryService.increaseArticle(category);
        Pointdispatchhistory point = new Pointdispatchhistory();
        point.setPointDispatchAmount(5);
        point.setPointDispatchDate(newDate);
        point.setPointDispatchReason("post a thread");
        point.setUser(user);
        pointService.createNewHistory(point);
        return this.listThreads();
    }


    public String newReply(){
        HttpServletRequest request = ServletActionContext.getRequest();
        String replyContent = request.getParameter("replyContent");

        System.out.println("this is the content of the reply: " + replyContent);

        return "reply";
    }
}
